package com.jackrain.nea.fc.dl.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.common.QueryCpUtil;
import com.jackrain.nea.config.Resources;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.dl.api.WmsDLTranBoxQueryCmd;
import com.jackrain.nea.fc.dl.mapper.WmsDLTranMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.QueryCmd;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;

/**
 * 拣货装箱单 发出
 * 收货店仓WMS管控 fc->wms
 *
 * @author kangcong
 * @date 2018/12/06
 */
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "dl")
public class WmsDLTranBoxQueryCmdImpl extends CommandAdapter implements WmsDLTranBoxQueryCmd {

    @Reference(version = "2", group = "cp")
    private QueryCmd queryCmd;

    @Autowired
    QueryCpUtil postQueryEvemt;

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {

//        log.debug("WmsDLTranBoxQueryCmdImpl-map-->{}",map);
        ValueHolder vh = new ValueHolder();
        JSONArray errorArroy=new JSONArray();
        WmsDLTranMapper wmsDLTranMapper = ApplicationContextHandle.getBean(WmsDLTranMapper.class);

        JSONObject param = JSON.parseObject(map.get("param").toString());
        log.info("WmsDLTranBoxQueryCmdImpl--param-->{}",param);

        if (!param.containsKey("IDS") && param.containsKey("DL_B_TRAN_BOX") ){
            param.put("IDS",param.getJSONArray("DL_B_TRAN_BOX"));
        }
        if (null == param || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }
        JSONArray dataArray = param.getJSONArray("IDS");
        JSONArray fixArray = new JSONArray();

        for (int i = 0; i < dataArray.size(); i++) {

            JSONObject fixcolumn = new JSONObject();
            JSONObject errorJson = new JSONObject();
            String idStr = dataArray.getString(i);
            HashMap mainMap = wmsDLTranMapper.selectTranBoxById(idStr);
//            log.debug("WmsDLTranBoxQueryCmdImpl-select-map--->{}",mainMap);
            if (mainMap == null) {
                errorJson.put("ID",idStr);
                errorJson.put("ERRORMSG","查询，数据为空");
//                log.info("WmsDLTranBoxQueryCmdImpl-selectTranBoxById-returnnull-->{}",idStr);
                errorArroy.add(errorJson);
               continue;
            }
            //查询主表
            JSONObject mainJo = new JSONObject(mainMap);
//            log.debug("WmsDLTranBoxQueryCmdImpl-select-jsonobject--->{}",mainJo);
            String bill_no=mainJo.getString("bill_no");
            //跨库查询
            Long storeid = mainJo.getLong("wms_store_code");
            if (null != storeid) {
                JSONObject whereJo = new JSONObject();
                whereJo.put("ID", storeid);
                JSONObject result = postQueryEvemt.queryCP("CP_C_STORE", "WMS_STORE_ECODE,ISWMS", whereJo);
//                log.info("WmsDLTranBoxQueryCmdImpl-querycp-result--->" , result);
                if (result != null) {
                    String wmsecode = TypeUtils.castToString(result.getString("WMS_STORE_ECODE"));
                    String iswms = TypeUtils.castToString(result.getString("ISWMS"));
                    if (!"1".equals(iswms)) {
                        errorJson.put("BILL_NO", bill_no);
                        errorJson.put("ERRORMSG","单据对应收货店仓不是WMS管控！");
                        errorArroy.add(errorJson);
//                        log.info("WmsDLTranBoxQueryCmdImpl-notiswms-->{}",bill_no);
                        continue;
                    }
                    mainJo.put("wms_store_code", wmsecode);
                } else {
                    mainJo.put("wms_store_code", null);
                }
            }
            mainJo.put("bill_date",mainJo.get("bill_date").toString());
            fixcolumn.put("mainData", mainJo);
            //查询子表
            List<HashMap> mapList = wmsDLTranMapper.selectwmstranintimesql(idStr);
            JSONArray jsonArray = new JSONArray();
            for (int a = 0; a < mapList.size(); a++) {
                HashMap hashMap = (HashMap) mapList.get(a);
                JSONObject jsonObject = new JSONObject(hashMap);
                jsonObject.put("bill_no", mainJo.getString("bill_no"));
                jsonArray.add(jsonObject);
            }
            fixcolumn.put("itemData", jsonArray);
            fixArray.add(fixcolumn);

        }
        vh.put("code", 0);
        if(errorArroy.size()>0){
            vh.put("code", -1);
            vh.put("errorArray", errorArroy);
        }
        vh.put("data", fixArray);
        vh.put("message", Resources.getMessage("成功"));
//        log.debug("WmsDLTranBoxQueryCmdImpl-return--->{}",vh);
        return vh;

    }

    /**
     * list TO jsonarra
     *
     * @param mapList
     * @return jsonArray
     */
    public JSONArray listToJsonarray(List<HashMap> mapList) {
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < mapList.size(); i++) {
            HashMap map = (HashMap) mapList.get(i);
            JSONObject jsonObject = new JSONObject(map);
            JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(jsonObject, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            jsonArray.add(param);
        }
        return jsonArray;
    }


}
